
--region Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[LineupDetails_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[LineupDetails_Insert]

IF OBJECT_ID(N'[dbo].[LineupDetails_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[LineupDetails_Update]

IF OBJECT_ID(N'[dbo].[LineupDetails_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[LineupDetails_Delete]

IF OBJECT_ID(N'[dbo].[LineupDetails_Select]') IS NOT NULL
	DROP PROCEDURE [dbo].[LineupDetails_Select]

--endregion

GO


--region [dbo].[LineupDetails_Select]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[LineupDetails_Select]
	@Page int = null,
	@PageSize int = null,
    @SortBy nvarchar(255) = null,
	@SortType bit = null,
	@IdLineupDetail int = null,
	@IdLineup int = null,
	@IdPlayer int = null,
	@Position nvarchar(50) = null,
	@NumberClothe int = null
AS

SET TRANSACTION ISOLATION LEVEL READ COMMITTED

Declare @RowCount int  
Set @RowCount = (
					Select Count (*) 
					From [dbo].[LineupDetails]
					WHERE
					(
						(@IdLineupDetail is null OR [LineupDetails].[IdLineupDetail] = @IdLineupDetail)
						AND (@IdLineup is null OR [LineupDetails].[IdLineup] = @IdLineup)
						AND (@IdPlayer is null OR [LineupDetails].[IdPlayer] = @IdPlayer)
						AND (@Position is null OR [LineupDetails].[Position] like @Position)
						AND (@NumberClothe is null OR [LineupDetails].[NumberClothe] = @NumberClothe)
					)
				)
if(@Page is null)
begin
	Set @Page = 1
end

if(@PageSize is null)
begin
	Set @PageSize = @RowCount
end

if(@SortBy is null)
begin
	Set @SortBy = 'DBNull'
end

if(@SortType is null)
begin
	Set @SortType = 1
end

SELECT *
FROM   (
			SELECT [dbo].[LineupDetails].*,
                   ROW_NUMBER() OVER 
                   (ORDER BY
                        CASE WHEN @SortBy = 'DBNull' THEN NewID() END ASC,
						CASE WHEN @SortBy = 'IdLineupDetail' and @SortType = 1 THEN [LineupDetails].[IdLineupDetail] END ASC,
						CASE WHEN @SortBy = 'IdLineupDetail' and @SortType = 0 THEN [LineupDetails].[IdLineupDetail] END DESC,
						CASE WHEN @SortBy = 'IdLineup' and @SortType = 1 THEN [LineupDetails].[IdLineup] END ASC,
						CASE WHEN @SortBy = 'IdLineup' and @SortType = 0 THEN [LineupDetails].[IdLineup] END DESC,
						CASE WHEN @SortBy = 'IdPlayer' and @SortType = 1 THEN [LineupDetails].[IdPlayer] END ASC,
						CASE WHEN @SortBy = 'IdPlayer' and @SortType = 0 THEN [LineupDetails].[IdPlayer] END DESC,
						CASE WHEN @SortBy = 'Position' and @SortType = 1 THEN [LineupDetails].[Position] END ASC,
						CASE WHEN @SortBy = 'Position' and @SortType = 0 THEN [LineupDetails].[Position] END DESC,
						CASE WHEN @SortBy = 'NumberClothe' and @SortType = 1 THEN [LineupDetails].[NumberClothe] END ASC,
						CASE WHEN @SortBy = 'NumberClothe' and @SortType = 0 THEN [LineupDetails].[NumberClothe] END DESC                   
                    ) AS RowNumber 
			FROM [dbo].[LineupDetails]
			Where 
			(
				(@IdLineupDetail is null OR [LineupDetails].[IdLineupDetail] = @IdLineupDetail)
				AND (@IdLineup is null OR [LineupDetails].[IdLineup] = @IdLineup)
				AND (@IdPlayer is null OR [LineupDetails].[IdPlayer] = @IdPlayer)
				AND (@Position is null OR [LineupDetails].[Position] like @Position)
				AND (@NumberClothe is null OR [LineupDetails].[NumberClothe] = @NumberClothe)
			)
		) AS Temp
WHERE  RowNumber Between (@Page * @PageSize - @PageSize + 1) And @Page * @PageSize
Return @RowCount


--end [dbo].[LineupDetails_Select]
--endregion

GO
--=========================================================================================--

	

--region [dbo].[LineupDetails_Insert]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[LineupDetails_Insert]
	@IdLineupDetail int OUTPUT,
	@IdLineup int,
	@IdPlayer int,
	@Position nvarchar(50),
	@NumberClothe int

AS


INSERT INTO [dbo].[LineupDetails] 
(
	[IdLineup],
	[IdPlayer],
	[Position],
	[NumberClothe]
)
VALUES 
(
	@IdLineup,
	@IdPlayer,
	@Position,
	@NumberClothe
)

SET @IdLineupDetail = SCOPE_IDENTITY()

--end [dbo].[LineupDetails_Insert]
--endregion

GO
--=========================================================================================--

--region [dbo].[LineupDetails_Update]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[LineupDetails_Update]
	@IdLineupDetail int,
	@IdLineup int,
	@IdPlayer int,
	@Position nvarchar(50),
	@NumberClothe int
AS


UPDATE [dbo].[LineupDetails] SET
	[IdLineup] = @IdLineup,
	[IdPlayer] = @IdPlayer,
	[Position] = @Position,
	[NumberClothe] = @NumberClothe
WHERE
	[IdLineupDetail] = @IdLineupDetail

--end [dbo].[LineupDetails_Update]
--endregion

GO
--=========================================================================================--

--region [dbo].[LineupDetails_Delete]

-- Create By: vdev124
-- Date Generated: Friday, March 04, 2011

CREATE PROCEDURE [dbo].[LineupDetails_Delete]
	@IdLineupDetail int
AS


DELETE FROM [dbo].[LineupDetails]
WHERE
(
	[IdLineupDetail] = @IdLineupDetail
)

--end [dbo].[LineupDetails_Delete]
--endregion

GO
--=========================================================================================--

